package com.ouc.openplatform.zykt.standardManage.service.impl;


import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouc.openplatform.zykt.standardManage.dto.NJ_TestBOMDTO;
import com.ouc.openplatform.zykt.standardManage.model.NJ_TestBOM;
import com.ouc.openplatform.zykt.standardManage.model.NJ_TestStandardUnion;
import com.ouc.openplatform.zykt.standardManage.service.NJ_ClientTestBOMService;
import com.ouc.openplatform.zykt.standardManage.service.NJ_TestBOMService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class NJ_ClientTestBOMServiceImpl implements NJ_ClientTestBOMService{
	
	NJ_TestBOMService testBOMService;
	
	public NJ_TestBOMService getTestBOMService(){
		return testBOMService;
	}
	
	public void setTestBOMService (NJ_TestBOMService testBOMService){
		this.testBOMService = testBOMService;
	}
	
	/**
	 * 对象转换方法
	 * */
	
	private NJ_TestBOMDTO transformTestBOMDTO (NJ_TestBOM testBOM){
		NJ_TestBOMDTO testBOMDTO = new NJ_TestBOMDTO();
		
		testBOMDTO.setId(testBOM.getId());
		testBOMDTO.setProductType(testBOM.getProductType());
		testBOMDTO.setTestType(testBOM.getTestType());
		testBOMDTO.setProductGrade(testBOM.getProductGrade());
		testBOMDTO.setVenture(testBOM.getVenture());
		testBOMDTO.setArea(testBOM.getArea());
		testBOMDTO.setStdCode(testBOM.getStdCode());
		testBOMDTO.setStdName(testBOM.getStdName());
		testBOMDTO.setStdItermCode(testBOM.getStdItermCode());
		testBOMDTO.setStdItermName(testBOM.getStdItermName());
		testBOMDTO.setStdSubitermCode(testBOM.getStdSubitermCode());
		testBOMDTO.setStdSubitermName(testBOM.getStdSubitermName());
		testBOMDTO.setOrderArry(testBOM.getOrderArry());
		testBOMDTO.setAddUser(testBOM.getAddUser());
		testBOMDTO.setAddUserName(testBOM.getAddUserName());
		testBOMDTO.setAddTime(testBOM.getAddTime());
		testBOMDTO.setMark(testBOM.getMark());
		return testBOMDTO;
	}
	
	private NJ_TestBOM transformTestBOM (NJ_TestBOMDTO testBOMDTO){
		NJ_TestBOM testBOM = new NJ_TestBOM();
		
		testBOM.setId(testBOMDTO.getId());
		testBOM.setProductType(testBOMDTO.getProductType());
		testBOM.setTestType(testBOMDTO.getTestType());
		testBOM.setProductGrade(testBOMDTO.getProductGrade());
		testBOM.setVenture(testBOMDTO.getVenture());
		testBOM.setArea(testBOMDTO.getArea());
		testBOM.setStdCode(testBOMDTO.getStdCode());
		testBOM.setStdName(testBOMDTO.getStdName());
		testBOM.setStdItermCode(testBOMDTO.getStdItermCode());
		testBOM.setStdItermName(testBOMDTO.getStdItermName());
		testBOM.setStdSubitermCode(testBOMDTO.getStdSubitermCode());
		testBOM.setStdSubitermName(testBOMDTO.getStdSubitermName());
		testBOM.setOrderArry(testBOMDTO.getOrderArry());
		testBOM.setAddUser(testBOMDTO.getAddUser());
		testBOM.setAddUserName(testBOMDTO.getAddUserName());
		testBOM.setAddTime(testBOMDTO.getAddTime());
		testBOM.setMark(testBOMDTO.getMark());
		return testBOM;
	}
	
	public JSONObject tsuToJsonObject (NJ_TestStandardUnion tsu){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("template_id", tsu.getTemplate_id());
		jsonObject.put("template_name", tsu.getTemplate_name());
		jsonObject.put("item_id", tsu.getItem_id());
		jsonObject.put("item_name", tsu.getItem_name());
		jsonObject.put("subitem_id", tsu.getSubitem_id());
		jsonObject.put("subitem_name", tsu.getSubitem_name());
		
		return jsonObject;
	}
	
	SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public JSONObject testBOMToJsonObject (NJ_TestBOM testBOM){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("id", testBOM.getId());
		jsonObject.put("productType", testBOM.getProductType());
		jsonObject.put("testType", testBOM.getTestType());
		jsonObject.put("productGrade", testBOM.getProductGrade());
		jsonObject.put("venture", testBOM.getVenture());
		jsonObject.put("area", testBOM.getArea());
		jsonObject.put("stdName", testBOM.getStdName());
		jsonObject.put("stdCode", testBOM.getStdCode());
		jsonObject.put("stdItermName", testBOM.getStdItermName());
		jsonObject.put("stdItermCode", testBOM.getStdItermCode());
		jsonObject.put("stdSubitermName", testBOM.getStdSubitermName());
		jsonObject.put("stdSubitermCode", testBOM.getStdSubitermCode());
		jsonObject.put("orderArry", testBOM.getOrderArry());
		jsonObject.put("addUser", testBOM.getAddUser());
		jsonObject.put("addUserName", testBOM.getAddUserName());
		try{
        	jsonObject.put("addTime", formatter.format(formatter.parse(testBOM.getAddTime())));
        }catch (Exception e) {
			// TODO: handle exception
		}
//		jsonObject.put("addTime", testBOM.getAddTime());
		jsonObject.put("mark", testBOM.getMark());
		
		return jsonObject;
	}
	
	
	//设置起止页
	public int[] setPage(String page,String rows,int total){
		int [] pArry = new int [3];
		pArry[2] = total;
		//当前页  
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数  
        int pageSize = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
		
		if (pArry[2]>0)
		{
			pArry[0]=(intPage-1) * pageSize+1;
			if (pArry[2]>intPage * pageSize)
			{
				pArry[1]=intPage * pageSize;
			}
			else
			{
				pArry[1]=pArry[2];
			}
		}
		else
		{
			pArry[0]=1;
			pArry[1]=pageSize;
		}
		return pArry;//返回起点和终点
	}
	
	@Override
	public Object searchAllTestBOM(String page,String rows) {
		int []pArry = setPage(page, rows, testBOMService.getSearchAllTestBOMCount());
		Map<String, Object> result = new HashMap<String, Object>(2);
		JSONArray jsonArray = new JSONArray();
		List<NJ_TestBOM> testBOMList = testBOMService.searchAllTestBOM(pArry[0],pArry[1]);
//		List<TestBOMDTO> testBOMDTOList = new ArrayList<TestBOMDTO>();
		for(NJ_TestBOM testBOM:testBOMList){
			jsonArray.add(testBOMToJsonObject(testBOM));
		}
		result.put("total", pArry[2]);
		result.put("rows", jsonArray);
		return JSONObject.fromObject(result);
	}

	@Override
	public Object searchTestBOMByCondition(String pd_type, String test_type,
			String test_grade, String venture,String area,String page,String rows) {
		// TODO Auto-generated method stub
		int []pArry = setPage(page, rows, testBOMService.getSearchTestBOMByConditionCount(
				pd_type, test_type, test_grade, venture, area));
		Map<String, Object> result = new HashMap<String, Object>(2);
		JSONArray jsonArray = new JSONArray();
		List<NJ_TestBOM> testBOMList = testBOMService.searchTestBOMByCondition(pd_type, 
				test_type, test_grade, venture, area, pArry[0], pArry[1]);
		for(NJ_TestBOM testBOM : testBOMList){
			jsonArray.add(testBOMToJsonObject(testBOM));
		}
		result.put("total", pArry[2]);
		result.put("rows", jsonArray);
		return JSONObject.fromObject(result);
	}

	@Override
	public int insertTestBOM(NJ_TestBOMDTO testBOMDTO) {
		System.out.println("enter testBOM insert testBOMDTO ProductGrade:"+testBOMDTO.getProductGrade());
		System.out.println("StdCode:"+testBOMDTO.getStdCode());
		System.out.println("StdName:"+testBOMDTO.getStdName());
		System.out.println("StdItermCode:"+testBOMDTO.getStdItermCode());
		System.out.println("StdItermName:"+testBOMDTO.getStdItermName());
		System.out.println("StdSubitermCode:"+testBOMDTO.getStdSubitermCode());
		System.out.println("StdSubitermName:"+testBOMDTO.getStdSubitermName());
		NJ_TestBOM testBOM = transformTestBOM(testBOMDTO);		
		return testBOMService.insertTestBOM(testBOM);
	}

	@Override
	public int editTestBOM(NJ_TestBOMDTO testBOMDTO) {
		NJ_TestBOM testBOM = transformTestBOM(testBOMDTO);
		return testBOMService.editTestBOM(testBOM);
	}

	@Override
	public int deleteTestBOM(int id) {
		
		return testBOMService.deleteTestBOM(id);
	}

	@Override
	public Object getAllTestBOM() {
		// TODO Auto-generated method stub
		List<NJ_TestBOM> testBomList = testBOMService.getAllTestBOM();
		JSONArray jsonArray = new JSONArray();
		for(NJ_TestBOM testBOM : testBomList){
			jsonArray.add(testBOMToJsonObject(testBOM));
		}
		return jsonArray;
	}

	@Override
	public Object getTestBOMByCondition(String pd_type, String test_type,
			String test_grade, String venture, String area) {
		// TODO Auto-generated method stub
		List<NJ_TestBOM> testBomList = testBOMService.getTestBOMByCondition(pd_type, test_type, test_grade, venture, area);
		JSONArray jsonArray = new JSONArray();
		for(NJ_TestBOM testBOM : testBomList){
			jsonArray.add(testBOMToJsonObject(testBOM));
		}
		return jsonArray;		
	}

	@Override
	public Object getAllTestStandardUnion() {
		// 
		List<NJ_TestStandardUnion> tsuList = testBOMService.getAllTestStandardUnion();
		JSONArray jsonArray = new JSONArray();
		for(NJ_TestStandardUnion testStandardUnion : tsuList){
			jsonArray.add(tsuToJsonObject(testStandardUnion));
		}
		return jsonArray;
	}

	@Override
	public Object getAllTestStandardUnionByCondition(int template_id,
			int item_id) {
		List<NJ_TestStandardUnion> tsuList = testBOMService.getAllTestStandardUnionByCondition(template_id, item_id);
		JSONArray jsonArray = new JSONArray();
		for(NJ_TestStandardUnion testStandardUnion : tsuList){
			jsonArray.add(tsuToJsonObject(testStandardUnion));
		}
		return jsonArray;
	}

	@Override
	public Object getAllTestStandardUnionByNames(String template_name,
			String item_name) {
		List<NJ_TestStandardUnion> tsuList = testBOMService.getAllTestStandardUnionByNames(template_name, item_name);
		JSONArray jsonArray = new JSONArray();
		for(NJ_TestStandardUnion testStandardUnion : tsuList){
			jsonArray.add(tsuToJsonObject(testStandardUnion));
		}
		return jsonArray;
	}



}
